package com.zeta.eduservice.controller;


import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zeta.commonutils.R;
import com.zeta.eduservice.entity.EduTeacher;
import com.zeta.eduservice.entity.EduTeachergroup;
import com.zeta.eduservice.entity.and.EduTeachergroupAndTeacher;
import com.zeta.eduservice.entity.vo.TeacherQuery;
import com.zeta.eduservice.entity.vo.TeacherVo;
import com.zeta.eduservice.service.EduTeachergroupAndTeacherService;
import com.zeta.eduservice.service.EduTeachergroupService;

import io.swagger.annotations.ApiParam;



/**
 * <p>
 * 教师组和教师中间表 前端控制器
 * </p>
 *
 * @author zeta
 * @since 2020-10-20
 */
@CrossOrigin
@RestController
@RequestMapping("/eduservice/edu_teachergroup")
public class EduTeachergroupController {

	@Autowired
	EduTeachergroupAndTeacherService teacherAndGroupService;
	@Autowired
	private EduTeachergroupService teacherGService;
	@PostMapping("pageStudentCondition/{page}/{limit}")
	public R getPage(@ApiParam(name = "page", value = "当前页码", required = true) @PathVariable Long page,

			@ApiParam(name = "limit", value = "每页记录数", required = true) @PathVariable Long limit ) {
          Page<EduTeachergroup> page2 = new Page<>(page,limit);
         teacherGService.page(page2, null);
         List<EduTeachergroup> records = page2.getRecords(); 
         long total = page2.getTotal();
         
		return R.ok().data("total",total).data("rows",records);
		
	}
	@PostMapping("")
	public R add(@RequestBody String s) {
		JSONObject parseObject = JSONObject.parseObject(s);
		EduTeachergroup eduTeachergroup = new EduTeachergroup();
		
		eduTeachergroup = parseObject.getObject("teacherGroupInfo",EduTeachergroup.class);
		teacherGService.save(eduTeachergroup);
		List<String> javaList = parseObject.getJSONArray("TeacherAddList").toJavaList(String.class);
		for(String i:javaList) {
			EduTeachergroupAndTeacher eduTeachergroupAndTeacher = new EduTeachergroupAndTeacher();
			eduTeachergroupAndTeacher.setTeachergroupId(eduTeachergroup.getId());
			eduTeachergroupAndTeacher.setTeacherId(i);
			teacherAndGroupService.save(eduTeachergroupAndTeacher);
			}
	
		return R.ok();
		
	}
	
	@DeleteMapping("{id}")
	public R delete(@PathVariable String id) {
		boolean removeById = teacherGService.removeById(id);
		return R.ok();
		
	}
	@PutMapping("")
	public R updata(@RequestBody EduTeachergroup teacherGroup) {
		teacherGService.updateById(teacherGroup);
		return R.ok();
		
	}
	@GetMapping("getByAcaId/{academyId}")
	public R getAll(@PathVariable String academyId) {
		QueryWrapper<EduTeachergroup> queryWrapper=new QueryWrapper<EduTeachergroup>();
		queryWrapper.eq("academy_id", academyId);
		List<EduTeachergroup> list = teacherGService.list(queryWrapper);
		return R.ok().data("list",list);
		
	}
	@GetMapping("{id}")
	public R get(@PathVariable String id) {

	EduTeachergroup byId = teacherGService.getById(id);
		return R.ok().data("teachergroup",byId);
		
	}
	@GetMapping("getTeacherList/{id}")
	public R getTeacherListById(@PathVariable String id) {
	 List<TeacherVo> teacherList = teacherGService.getTeacherList(id);
		return R.ok().data("teacherList",teacherList);
		
	}
	@GetMapping("getByTeacherId/{teacherId}")
	public R getAllByTeacherId(@PathVariable String teacherId) {
		List<EduTeachergroup> list = teacherGService.getListByTeacherId(teacherId);
		return R.ok().data("list",list);
		
	}
	@PostMapping("pageTeacherGroupByGroupId/{current}/{limit}/{GroupId}")//requestbody必须用 post   required 这个值可以没用
	public R PageTeacherConditionByclass(@PathVariable long current, @PathVariable long limit,@PathVariable String GroupId) {
		Page<EduTeacher> page = new Page<>(current, limit);
		List<EduTeacher> listByGroupId = teacherGService.getListByGroupId(page,GroupId);
		long total = page.getTotal();
		return R.ok().data("total", total).data("rows", listByGroupId);
	}
}

